var PaymentModel = function (paymentMap, warehouseList, type) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (paymentMap, warehouseList, type) {
      var warehouses = [];
      var fields = ['aleradyApprovalPrice', 'notApprovalPrice', 'aleradyApprovalAddReducePrice', 'notApprovalAddReducePrice'];
      for (var i = 0; i < warehouseList.length; i++) {
        var warehouse = warehouseList[i];
        var id = type === 'order' ? warehouse.typeId : warehouse.id;
        var obj = {
          id: id,
          warehouse: warehouse,
          addReducePrice: undefined,
          addReduceType: 0,
          addReduceReason: ''
        };
        var payment = paymentMap[id];
        if (payment === undefined) {
          payment = {
            aleradyApprovalPrice: 0,
            notApprovalPrice: 0,
            aleradyApprovalAddReducePrice: 0,
            notApprovalAddReducePrice: 0
          };
        }
        SysUtil.setPostData(obj, payment, fields);

        var totalPrice = warehouse.totalPrice;
        var payedPrice = payment.aleradyApprovalPrice + payment.notApprovalPrice;
        var notPayPrice = totalPrice - payedPrice;
        if (notPayPrice < 0) {
          notPayPrice = 0;
        } else {
          notPayPrice = _.round(notPayPrice, 2);
        }
        obj.payedPrice = _.round(payedPrice, 2);
        obj.notPayPrice = notPayPrice;
        obj.payPrice = notPayPrice;
        obj.totalPrice = totalPrice;
        warehouses.push(obj);
      }

      self.warehouseList = warehouses;
      self.receiptPaymentMode = '';
      self.conversionPrice = undefined;
      self.relationBank = '';
      self.reason = '';
      self.invoicePrice = undefined;
      self.invoiceDate = null;
      self.actualDay = moment().valueOf();
      self.totalAddReducePrice = _.round(_.sumBy(warehouses, 'aleradyApprovalAddReducePrice'), 2);
      self.totalNotApprovalAddReducePrice = _.round(_.sumBy(warehouses, 'notApprovalAddReducePrice'), 2);

      var customerName = '';
      if (type === 'order') {
        customerName = warehouseList[0].supplierName;
      } else {
        customerName = warehouseList[0].providerName;
      }
      self.customerName = customerName;
    }
  };

  this.getMaxPayPrice = function (warehouse) {
    var addReduceType = warehouse.addReduceType;
    var notPayPrice = warehouse.notPayPrice;
    if (addReduceType === 0) {
      return notPayPrice;
    }

    var addReducePrice = warehouse.addReducePrice;
    if (addReducePrice === undefined) {
      addReducePrice = 0;
    }

    var maxPayPrice = 0;
    if (addReduceType === 1) {
      maxPayPrice = notPayPrice + addReducePrice;
    } else {
      maxPayPrice = notPayPrice - addReducePrice;
    }
    return _.round(maxPayPrice, 4);
  };

  this.getMaxReducePrice = function (warehouse) {
    var addReduceType = warehouse.addReduceType;
    if (addReduceType === 1) {
      return Infinity;
    }

    var payPrice = warehouse.payPrice;
    if (payPrice === undefined) {
      payPrice = 0;
    }
    var notPayPrice = warehouse.notPayPrice;
    var maxMomey = notPayPrice - payPrice;
    return _.round(maxMomey, 4);
  };

  this.getSumPayPrice = function () {
    var warehouseList = this.warehouseList;
    var sumPayPrice = _.sumBy(warehouseList, function (warehouse) {
      var payPrice = warehouse.payPrice;
      if (payPrice === undefined) {
        payPrice = 0;
      }
      return payPrice;
    });
    var conversionPrice = this.conversionPrice;
    if (conversionPrice === undefined) {
      conversionPrice = 0;
    }
    return _.round(sumPayPrice - conversionPrice, 4);
  };

  this.checkValid = function (isAdvance) {
    var fields = ['receiptPaymentMode'];
    if (!isAdvance) {
      fields.push('relationBank');
    }

    var code = this.checkRequiredFields(fields);
    if (code !== '') {
      return code;
    }

    var invoicePrice = this.invoicePrice;
    var invoiceDate = this.invoiceDate;
    if (!SysUtil.isEmpty(invoicePrice) || !SysUtil.isEmpty(invoiceDate)) {
      if (invoicePrice === undefined) {
        return 'invoicePrice';
      }
      if (invoiceDate === null) {
        return 'invoiceDate';
      }
    }

    var warehouseList = this.warehouseList;
    for (var i = 0; i < warehouseList.length; i++) {
      var warehouse = warehouseList[i];
      var payPrice = warehouse.payPrice;
      if (payPrice === undefined) {
        return 'payPrice';
      }

      var addReduceType = warehouse.addReduceType;
      var addReducePrice = warehouse.addReducePrice;
      // if (addReduceType !== 2 || addReducePrice !== warehouse.notPayPrice) {
      //   if (payPrice === 0) {
      //     return 'payPriceZero';
      //   }
      // }

      if (addReduceType === 0) {
        continue;
      }

      if (addReducePrice === undefined) {
        return 'addReducePrice';
      }
      if (addReducePrice === 0) {
        return 'addReducePriceZero';
      }

      if (warehouse.addReduceReason.trim() === '') {
        return 'addReduceReason';
      }
    }

    return '';
  };

  this.getObject = function (payType, type) {
    var postData = {
      type: type === 'out' ? 1 : 2,
      payType: payType,
      receiptPaymentMode: this.receiptPaymentMode,
      relationBank: this.relationBank,
      customerName: this.customerName,
      reason: this.reason
    }

    var conversionPrice = this.conversionPrice;
    if (conversionPrice === undefined) {
      conversionPrice = 0;
    }
    postData.conversionPrice = _.round(conversionPrice, 4);

    if (type === 'order') {
      postData.isPutOut = 1;
      postData.source = 1;
    }

    var invoicePrice = this.invoicePrice;
    var invoiceDate = this.invoiceDate;
    if (!SysUtil.isEmpty(invoicePrice) && !SysUtil.isEmpty(invoiceDate)) {
      postData.invoicePrice = invoicePrice;
      postData.invoiceDate = moment(invoiceDate).format('YYYY-MM-DD');
    }

    var actualDay = this.actualDay;
    if (!SysUtil.isEmpty(actualDay)) {
      postData.actualDay = moment(actualDay).format('YYYY-MM-DD HH:mm:ss');
    }

    var warehouseList = this.warehouseList;
    var sumPayPrice = 0;
    var details = [];
    for (var i = 0; i < warehouseList.length; i++) {
      var warehouse = warehouseList[i];
      var addReduceType = warehouse.addReduceType;
      var payPrice = warehouse.payPrice;
      sumPayPrice += payPrice;
      var detail = {
        inOutId: warehouse.id,
        price: payPrice,
        addReduceType: addReduceType
      };
      if (type === 'order') {
        detail.isPutOut = 1;
        detail.source = 1;
      }
      if (addReduceType !== 0) {
        detail.addReducePrice = warehouse.addReducePrice;
        detail.addReduceReason = warehouse.addReduceReason;
      } else {
        detail.addReducePrice = 0;
        detail.addReduceReason = '';
      }

      details.push(detail);
    }
    postData.detailList = details;

    var sumMoney = sumPayPrice - conversionPrice;
    postData.sumMoney = _.round(sumMoney, 4);

    return postData;
  };

  fn.initData(paymentMap, warehouseList, type);
};
